package com.oocl.Services;

import com.oocl.Bean.Customer;
import com.oocl.Model.LinkedList;

public class ListServices {
	
	private com.oocl.Model.LinkedList<Customer> list;
	
	public ListServices(){
		list=new com.oocl.Model.LinkedList<Customer>();
	}
	
	public com.oocl.Model.LinkedList<Customer>getList(){
		return list;
	}
	
	/**
	 * initial the list with 10 record
	 */
	public void initial(){
		Customer c1=new Customer("1", "aa", "man", "2000-01-01", "Beijing", "123456");
		Customer c2=new Customer("2", "bb", "woman", "2000-01-02", "Tianjin", "123456");
		Customer c3=new Customer("3", "cc", "woman", "2000-01-03", "Beijing", "123456");
		Customer c4=new Customer("4", "dd", "man", "2000-01-04", "zhuhai", "123456");
		Customer c5=new Customer("5", "ee", "man", "2000-01-05", "zhuhai", "123456");
		Customer c6=new Customer("6", "ab", "woman", "2000-02-05", "guiling", "123456");
		Customer c7=new Customer("7", "bc", "woman", "2000-03-05", "qinghai", "123456");
		Customer c8=new Customer("8", "cd", "man", "2000-04-05", "xizang", "123456");
		Customer c9=new Customer("9", "de", "woman", "2000-05-05", "zhuhai", "123456");
		Customer c10=new Customer("10", "ff", "man", "2000-05-06", "zhuhai", "123456");
		list.insertNode(c1);
		list.insertNode(c2);
		list.insertNode(c3);
		list.insertNode(c4);
		list.insertNode(c5);
		list.insertNode(c6);
		list.insertNode(c7);
		list.insertNode(c8);
		list.insertNode(c9);
		list.insertNode(c10);
		
		System.out.println( "++++++++++++++++++++++++++++++++++++++" );
        System.out.println( "+       Welcome to CRM System!       +" );
        System.out.println( "++++++++++++++++++++++++++++++++++++++\n" );
		System.out.println("Now there are 10 customers in the system!\n");
		printAll();
		help(null);
	}
	
	/**
	 * print out all the customers' record in the list
	 */
	public void printAll(){
		System.out.println("ID \tNAME \tSEX \tBIRTHDAY \tADDRESS \tCELL \n"
				+ "--------------------------------------------------------------");
		for(int i=0; i<list.getLength(); i++){
			Customer customer = list.getNode(i).getValue();
			System.out.println(customer.toString());
		}
	}
	
	/**
	 * print out one of the record according to the index
	 * @param index
	 */
	public void print(int index) {
		Customer customer = list.getNode(index).getValue();
		System.out.println(customer.toString());
	}
	
	/**
	 * To check record in the system
	 * @param str
	 */
	public void look(String str) {
		if(str == null){
			printAll();
		}else {
			String[] arr = str.split(",");
			String[] arr2 = arr[0].split(":");
			if(arr2[0].equals("id")){
				String namestring = arr2[1];
				for(int i=0; i<list.getLength(); i++){
					if(list.getNode(i).getValue().getId().equals(namestring)){
						print(i);
					}
				}
			}else{
				String nameString = arr2[1];
				for(int i=0; i<list.getLength(); i++){
					if(list.getNode(i).getValue().getName().equals(nameString)){
						print(i);
					}
				}
			}
		}
	}
	
	/**
	 * To add records into the system
	 * @param str
	 */
	public void add(String str){
		if(str != null){
			String[] array = str.split(",");
			String[][] parameter = new String[array.length][2];
			for(int i=0; i<array.length; i++){
				parameter[i] = array[i].split(":");
			}
			Customer c = new Customer();
			c.setId((list.getLength()+1)+"");
			for(int i=0; i<array.length; i++){
				if(parameter[i][0].equals("name")){
					c.setName(parameter[i][1]);
				}else{
					c.setName(null);
				}
			}
			for(int i=0; i<array.length; i++){
				if(parameter[i][0].equals("sex")){
					c.setSex(parameter[i][1]);
				}else{
					c.setSex(null);
				}
			}
			for(int i=0; i<array.length; i++){
				if(parameter[i][0].equals("birthday")){
					c.setBirthday(parameter[i][1]);
				}else{
					c.setBirthday(null);
				}
			}
			for(int i=0; i<array.length; i++){
				if(parameter[i][0].equals("address")){
					c.setAddress(parameter[i][1]);
				}else{
					c.setAddress(null);
				}
			}
			for(int i=0; i<array.length; i++){
				if(parameter[i][0].equals("cell")){
					c.setCell(parameter[i][1]);
				}else{
					c.setCell(null);
				}
			}
			list.insertNode(c);
		}
	}
	
	/**
	 * To delete the record in the system
	 * @param str
	 */
	public void delete(String str){
		if(str != null){
			list.delteNode(Integer.parseInt(str));
			System.out.println("delete the customer whose id is:"+Integer.parseInt(str));
		}
	}
	
	/**
	 * To modify the record in the system
	 * @param str
	 */
	public void modify(String str){
		String[] a = str.split(" ");
		String[] array = a[1].split(",");
		Customer customer = list.getNode(Integer.parseInt(a[0])-1).getValue();
		String[][] parameter = new String[array.length][2];
		for(int i=0; i<array.length; i++){
			parameter[i] = array[i].split(":");
		}
		for(int i=0; i<array.length; i++){
			if(parameter[i][0].equals("name")){
				customer.setName(parameter[i][1]);
			}
		}
		for(int i=0; i<array.length; i++){
			if(parameter[i][0].equals("sex")){
				customer.setSex(parameter[i][1]);
			}
		}
		for(int i=0; i<array.length; i++){
			if(parameter[i][0].equals("birthday")){
				customer.setBirthday(parameter[i][1]);
			}
		}
		for(int i=0; i<array.length; i++){
			if(parameter[i][0].equals("address")){
				customer.setAddress(parameter[i][1]);
			}
		}
		for(int i=0; i<array.length; i++){
			if(parameter[i][0].equals("cell")){
				customer.setCell(parameter[i][1]);
			}
		}
		System.out.println("modify finished!");
		print(Integer.parseInt(a[0])-1);
	}
	
	/**
	 * To sort all the records in the list(without change the actually date) 
	 * @param str
	 */
	public LinkedList<Customer> sort(String str){
		if(str.equals("id")){
			printAll();
			return list;
		}else{
			com.oocl.Model.LinkedList<Customer> copyList = new com.oocl.Model.LinkedList<Customer>();
			for(int i=0; i<list.getLength(); i++){
				Customer customer=list.getNode(i).getValue();
				copyList.insertNode(customer);
			}
			copyList.sort();
			return copyList;
		}
	}
	
	
	public void printSorted(com.oocl.Model.LinkedList<Customer> sortedList) {
		System.out.println("ID \tNAME \tSEX \tBIRTHDAY \tADDRESS \tCELL \n"
				+ "--------------------------------------------------------------");
		for(int i=0; i<sortedList.getLength(); i++){
			Customer customer = sortedList.getNode(i).getValue();
			System.out.println(customer.toString());
		}
	}

	/**
	 * Get the size of the list
	 * @return
	 */
	public int size(){
		return list.getLength();
	}
	
	/**
	 * The command and the introduction of the command
	 * @param str
	 */
	public void help(String str){
		if(str == null){
			System.out.println("\nHelp\n-- Look: L [id:xx/name:xx,sex:man/woman,birthday:YYYY-MM-DD,address:xx,cell:xx] \n"
					+ "-- Add: A name:xx[,sex:man/woman,birthday:YYYY-MM-DD,address:xx,cell:xx] \n"
					+ "-- Delete: D id \n"
					+ "-- Modify: U id name:xx[,sex:man/woman,birthday:YYYY-MM-DD,address:xx,cell:xx] \n"
					+ "-- Sort: O name/id \n"
					+ "-- Help: H [L/A/D/U/O/Q]\n"
					+ "-- Quit: Q");
		}else{
			switch (str) {
			case "A":
				System.out.println("Add: A name:xx[,sex:man/woman,birthday:YYYY-MM-DD,address:xx,cell:xx]");
				break;
			case "L":
				System.out.println("Look: L [id:xx/name:xx,sex:man/woman,birthday:YYYY-MM-DD,address:xx,cell:xx]");
				break;
			case "D":
				System.out.println("Delete: D id");
				break;
			case "O":
				System.out.println("Sort: O name/id");
				break;
			case "Q":
				System.out.println("Quit: Q");
				break;
			case "H":
				System.out.println("Help: H [L/A/D/U/O/Q]");
				break;
			case "U":
				System.out.println("Modify: U id name:xx[,sex:man/woman,birthday:YYYY-MM-DD,address:xx,cell:xx]");
				break;
			default:
				break;
			}
		}
		
	}
}
